package cn.netjava.robo3d.device;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;

import cn.netjava.robo3d.WarCar;

import com.jme.input.InputHandler;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jmex.awt.swingui.JMEDesktop;

/**
 * 游戏控制桌面，（导航界面）,它将布满整个屏幕
 * @author westice
 *
 */
public class ControlDesk extends Node{

	private JMEDesktop desktop;
	private JDesktopPane desktopPane;
	private DisplaySystem display;
	private int displaywide;
	private int displayheight;
	private boolean showcursor=true;
	//y代表显示的“隐藏鼠标”，x代表显示的“按o键显示鼠标”
	private char showcursortxt='y';
	private boolean showatlas=true;
	private JButton showcursorbutton;
	private JButton showatlasbutton;
	private JButton changemodebutton;
	private JLabel helplabel;

	private WarCar player;
	private Point mouseloc;
	private JLabel cross;
	private int crosssize=40;
	private float speed=0.015f;  //鼠标控制准星的速度
	boolean hascross=false;


	public ControlDesk(InputHandler input,WarCar player){
		this.player=player;
		display=DisplaySystem.getDisplaySystem();
		displaywide=display.getWidth();
		displayheight=display.getHeight();
		desktop=new JMEDesktop("controldesk");
		desktop.getJDesktop().setBackground(new Color(0,0,0,0));//完全透明
		desktop.setup(displaywide,displayheight,false,input);
		desktop.setLightCombineMode(Spatial.LightCombineMode.Off);
		desktop.setCullHint(Spatial.CullHint.Never);
		this.attachChild(desktop);
		desktopPane=desktop.getJDesktop();
		mouseloc=new Point();

		this.setRenderQueueMode(Renderer.QUEUE_ORTHO);//这个设置让挡住的得以看见
		this.setCullHint(Spatial.CullHint.Never);

		this.setLocalTranslation(displaywide/2, displayheight/2, 0 );
		createElements();
		addmouseaction();

	}

	//创建桌面元素
	private void createElements(){
		showcursorbutton=new JButton("隐藏鼠标");
        showcursorbutton.setSize(displaywide/8, displayheight/23);
        showcursorbutton.setLocation(displaywide*87/100, displayheight*95/100);

        showcursorbutton.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
        	    //System.out.println("监听有效 showcursortxt:"+showcursortxt+"---showcursor:"+showcursor);
            	 showcursor=!showcursor;
            }

            public void mousePressed(MouseEvent e){

            }

            public void mouseReleased(MouseEvent e){

            }

            public void mouseEntered(MouseEvent e){

            }

            public void mouseExited(MouseEvent e){

            }
        });
        desktopPane.add(showcursorbutton);

        showatlasbutton=new JButton("显示雷达地图(M)");
        showatlasbutton.setSize(displaywide/8, displayheight/23);
        showatlasbutton.setLocation(displaywide*72/100, displayheight*95/100);
        showatlasbutton.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
        	    //System.out.println("监听有效 showcursortxt:"+showcursortxt+"---showcursor:"+showcursor);
            	showatlas=!showatlas;
            }

            public void mousePressed(MouseEvent e){

            }

            public void mouseReleased(MouseEvent e){

            }

            public void mouseEntered(MouseEvent e){

            }

            public void mouseExited(MouseEvent e){

            }
        });
        desktopPane.add(showatlasbutton);

        changemodebutton=new JButton("射击模式(V)");
        changemodebutton.setSize(displaywide/8, displayheight/23);
        changemodebutton.setLocation(displaywide*72/100, displayheight*90/100);
        changemodebutton.addMouseListener(new MouseListener(){
        	public void mouseClicked(MouseEvent e){
                 if(player.getViewmode()==WarCar.chase){
                	 player.setViewmode(WarCar.scope);
                 }else{
                	 player.setViewmode(WarCar.chase);
                 }
            }

            public void mousePressed(MouseEvent e){

            }

            public void mouseReleased(MouseEvent e){

            }

            public void mouseEntered(MouseEvent e){

            }

            public void mouseExited(MouseEvent e){

            }
        });
        desktopPane.add(changemodebutton);

        cross=new JLabel(){
        	protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.pink);
        	    g2d.drawLine(0,crosssize/2,crosssize,crosssize/2);
        	    g2d.drawLine(crosssize/2,0,crosssize/2,crosssize);
//                g2d.drawOval(200, 200, 50, 60);
                g2d.dispose();
        	}
        };
        cross.setSize(crosssize, crosssize);
        cross.setBackground(new Color(0,0,0,0));
        cross.setLocation(displaywide/2-crosssize/2, displayheight/2-crosssize/2);
        cross.setVisible(false);

        JButton changegear=new JButton("切换油门档(C)");
        changegear.setSize(displaywide/8, displayheight/23);
        changegear.setLocation(displaywide*58/100, displayheight*95/100);
        changegear.addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
                 player.changeGear();
			}

			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}
        });
        desktopPane.add(changegear);

        JButton helpbutton=new JButton("帮助(help)");
        helpbutton.setSize(displaywide/8, displayheight/23);
        helpbutton.setLocation(displaywide*58/100, displayheight*90/100);
        helpbutton.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
                if(helplabel.isVisible()){
                	helplabel.setVisible(false);
                }else{
                	helplabel.setVisible(true);
                }
			}
	
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}
			
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}
			
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}
        });
        desktopPane.add(helpbutton);

        helplabel=new JLabel(" 这是坦克对战游戏(java版)\n\r base on JMonkeyEngine");
        helplabel.setSize(displaywide/4, displayheight/2);
        helplabel.setBackground(new Color(0.1f,0.1f,0.8f,0.2f));
        helplabel.setLocation(displaywide*1/2,  displayheight*1/2);
        helplabel.setVisible(false);
        desktopPane.add(helplabel);


	}

	public void update(){
		//System.out.println("更新controldesk");
   	 if(showcursor&&showcursortxt=='x'){
   	   showcursortxt='y';
  	   showcursorbutton.setText("隐藏鼠标");
  	   //System.out.println("改变成: 隐藏鼠标");
  	 }
  	 if((!showcursor)&&showcursortxt=='y')
  	 {
  	   showcursortxt='x';
       showcursorbutton.setText("按o键显示鼠标");
       //System.out.println("改变成: 按o键显示鼠标");
  	 }


  	 if(player.getViewmode()==WarCar.scope){
  		 if(!hascross){
    		   desktopPane.add(cross);
    		   hascross=!hascross;
    		 }
   		 if(!cross.isVisible()){
  			cross.setVisible(true);
  		 }

  	 }
  	 if(player.getViewmode()==WarCar.chase){
  		 if(cross.isVisible()){
  			 cross.setVisible(false);
  		 }
  	 }

	}

	//给桌面加鼠标行为
	private void addmouseaction(){
		desktopPane.addMouseListener(new MouseListener(){
			 public void mouseClicked(MouseEvent e){
	        	   // System.out.println("鼠标点击了桌面");
	               //坦克射击
				   player.shoot();

	            }

	            public void mousePressed(MouseEvent e){

	            }

	            public void mouseReleased(MouseEvent e){

	            }

	            public void mouseEntered(MouseEvent e){

	            }

	            public void mouseExited(MouseEvent e){

	            }
		});

		desktopPane.addMouseMotionListener(new MouseMotionListener(){
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			int mX=displaywide/2;
			boolean right=true;

			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				mouseloc=e.getLocationOnScreen();
				if(player.getViewmode()==WarCar.scope){
//					System.out.println("鼠标移动了"+(i++));
				    if(mouseloc.x<mX){
				    	//鼠标向左运动
				    	right=false;
				    }
				    if(mouseloc.x>mX){
				    	//鼠标向右运动
				    	right=true;
				    }
				    if(mouseloc.x==mX){
				    	if(mouseloc.x<displaywide/2){
				    		////鼠标向左运动
				    		right=false;
				    	}else{
				    	   ////鼠标向右运动
				    		right=true;
				    	}
				    }

				   float theta=0;   //旋转角度
				   if(right){
					   //System.out.println("鼠标向右移动");
					   //此时向量按坐标正方向旋转
					   theta=theta+speed;
				   }else{
					   //System.out.println("鼠标向左移动");
				       theta=theta-speed;
				   }
				   if(theta>2*FastMath.PI){
					   theta=theta-2*FastMath.PI;
				   }
				   if(theta<-2*FastMath.PI){
					   theta=theta+2*FastMath.PI;
				   }
				   //将主角原来的gun方向旋转theta
				   float x1=player.getgundirection().x;
				   float z1=player.getgundirection().z;
				   float y1=player.getgundirection().y;
				   float x2=x1*FastMath.cos(theta)-z1*FastMath.sin(theta);
				   float z2=x1*FastMath.sin(theta)+z1*FastMath.cos(theta);
				   player.setgundirection(new Vector3f(x2,y1,z2));
				    mX=mouseloc.x;
				}
			}

		});


	}

	public boolean isShowcursor() {
		return showcursor;
	}

	public void setShowcursor(boolean showcursor) {
		this.showcursor = showcursor;
	}

	public boolean isShowatlas() {
		return showatlas;
	}

	public void setShowatlas(boolean showatlas) {
		this.showatlas = showatlas;
	}

	public Point getMouseloc() {
		return mouseloc;
	}

	public void setMouseloc(Point mouseloc) {
		this.mouseloc = mouseloc;
	}


}
